var express = require('express');
var router = express.Router();

var db = require('../db.js');
var {

    find,
    insert,
    update,
    del,
    search,
    skip

} = db




/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('index.html', {
        title: 'Express'
    });
});

router.get('/index/indexDetail', function (req, res, next) {
    res.render('index/indexDetail.html', {
        title: 'Express'
    });
});



//首页接收登录数据
router.post('/', function (req, res, next) {
    console.log(req.body);
});

//关于我们页面
router.get('/about', function (req, res, next) {
    res.render('about.html', {
        title: 'Express'
    });
});
//关于我们二级路由
//企业新闻页面
router.get('/about/news', function (req, res, next) {
    res.render('about/news.html', {
        title: 'Express'
    });
});
//企业新闻页面
router.get('/about/responsibility', function (req, res, next) {
    res.render('about/responsibility.html', {
        title: 'Express'
    });
});
//企业新闻页面
router.get('/about/history', function (req, res, next) {
    res.render('about/history.html', {
        title: 'Express'
    });
});
//企业新闻页面
router.get('/about/contact', function (req, res, next) {
    res.render('about/contact.html', {
        title: 'Express'
    });
});

//菜单页面

router.get('/menu', async function (req, res, next) {
    let menuData = await find('menu')
    res.render('menu.html', {
        menuData
    });
});
//菜单页面二级路由
//饮料



router.get('/menu/beverages', async function (req, res, next) {
    let beveragesData = await find('beverages')
    res.render('menu/beverages.html', {
        beveragesData
    })
})

//咖啡产品
router.get('/menu/coffee', function (req, res, next) {
    res.render('menu/coffee.html', {
        title: 'Express'
    })
});

router.get('/menu/beverages', function (req, res, next) {
    res.render('menu/beverages.html', {
        title: 'Express'
    });

});
// 美食deliciousFood
router.get('/menu/deliciousFood', function (req, res, next) {
    res.render('menu/deliciousFood.html', {
        title: 'Express'
    });
});

//礼卡页面

router.get('/stargiftcard', async function (req, res, next) {
    var giftcard = await db.find('giftcard');
    var tmall = await db.find('tmall');
    var scancode = await db.find('scancode');
    var company = await db.find('company');
    var series = await db.find('series');
    res.render('stargiftcard.html', {
        // title: 'Express'
        giftcard: giftcard,
        tmall: tmall,
        scancode: scancode,
        company: company,
        series: series
    });
});

//甄选页面
router.get('/stratbucks_selected', function (req, res, next) {
    res.render('stratbucks_selected.html', {
        title: 'Express'
    });
});

//帮助页面
router.get('/help',async function (req, res, next) {
  var left = await find('left')
  var lefts = await find('left')
  lefts.shift()
  var allAnswer = await find('allAnswer')
  var AnswerOne = await find('AnswerOne')
  var AnswerThree = await find('AnswerThree')
  var AnswerFour = await find('AnswerFour')
  var AnswerFive = await find('AnswerFive')
  var AnswerSix = await find('AnswerSix')
  var AnswerSeven = await find('AnswerSeven')
  var AnswerEight = await find('AnswerEight')
  var AnswerTwo = await find('AnswerTwo')
  var [AnswerTwo1,AnswerTwo2,AnswerTwo3] = Intercept(AnswerTwo)
  var [AnswerFour1,AnswerFour2,AnswerFour3] = Intercept(AnswerFour)
  function Intercept(arr){
    var arr1 = []
    var arr2 = []
    var arr3 = []
    arr.forEach((i,index) => {
      if(i.doubt[0]=='1'){
        arr1.push(i)
      }else if(i.doubt[0]=='2'){
        arr2.push(i)
      }else{
        arr3.push(i)
      }
    });
    return [arr1,arr2,arr3]
  }
  var commonProblem = await find('commonProblem')
  res.render('help.html', {
    title: 'Express',
    left,
    lefts,
    allAnswer,
    AnswerOne,
    AnswerThree,
    AnswerFive,
    AnswerSix,
    AnswerSeven,
    AnswerEight,
    AnswerTwo1,
    AnswerTwo2,
    AnswerTwo3,
    AnswerFour1,
    AnswerFour2,
    AnswerFour3,
    commonProblem,
  });
});
router.get('/help/searsh',async function (req, res, next) {
    var {searchText} = req.query
    var searchAll = await search('allAnswer','doubt',searchText)
    var search1 = await search('AnswerOne',"doubt",searchText)
    var search2 = await search('AnswerTwo',"doubt",searchText)
    var search3 = await search('AnswerThree',"doubt",searchText)
    var search4 = await search('AnswerFour',"doubt",searchText)
    var search5 = await search('AnswerFive',"doubt",searchText)
    var search6 = await search('AnswerSix',"doubt",searchText)
    var search7 = await search('AnswerSeven',"doubt",searchText)
    var search8 = await search('AnswerEight',"doubt",searchText)
    var searchsAll = searchAll.concat(search1).concat(search2).concat(search3).concat(search4).concat(search5).concat(search6).concat(search7).concat(search8) 
  
    function ArrSet(Arr, id) {
        var obj = {};
        const arrays = Arr.reduce((setArr, item) => {
          obj[item[id]] ? '' : obj[item[id]] = true && setArr.push(item);
          return setArr;
        }, []);
        return arrays;
    }
    var searchResult = ArrSet(searchsAll, 'doubt')
    res.send(searchResult);
});

//登录页面
router.get('/login', function (req, res, next) {
    res.render('login.html', {
        title: 'Express'
    });
});

//注册页面
router.get('/register', function (req, res, next) {
    res.render('register.html', {
        title: 'Express'
    });
});

//忘记密码页面
router.get('/forget', function (req, res, next) {
    res.render('forget.html', {
        title: 'Express'
    });
});




//星享俱乐部页面
router.get('/club', function (req, res, next) {
    res.render('club.html', {
        title: 'Express'
    });
});




//星礼卡二级页面管理星礼卡路由
router.get('/manage', function (req, res, next) {
    res.render('manage.html', {})
});


//星礼卡管理星卡查询路由
router.post('/manage_password', async function (req, res, next) {
    console.log(req.body);
    var {
        id,
        password
    } = req.body
    id = Number(id)
    console.log(id);
    password = Number(password)
    var jg = await find('users', {
        id
    })
    if (jg.length == 0) {

    } else {
        if (jg[0].password == password) {
            console.log(jg);
            res.send({
                jg: jg[0]
            })
        }
    }
});

//星礼卡管理星卡查询路由
router.post('/manage_code', async function (req, res, next) {
    var {
        code,
        id
    } = req.body
    code = Number(code)
    id = Number(id)
    var jg = await find('users', {
        id
    })
    if (jg.length == 0) {

    } else {
        if (jg[0].code == code) {
            res.send({
                jg: jg[0]
            })
        }
    }

});


//星礼卡二级页面，发票管理页面路由
router.get('/bill', function (req, res, next) {
    res.render('bill.html', {});
});

//星礼卡三级页面，星礼卡章程页面路由
router.get('/bylaws', function (req, res, next) {
    res.render('bylaws.html', {});
});


//搜索页面

router.get('/menuSearch', async function (req, res, next) {
    let {
        val
    } = req.query
    let result = await search('menu', 'content', val)
    let content = JSON.parse(result[0].content)
    res.send(content)

});

module.exports = router;